﻿using System;
using System.Collections.Generic;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class MovingBusiness
    {
        private const string Waming = "Business.MovingBusiness";
        private readonly UserMovingDAL userMovingDal = new UserMovingDAL();

        /// <summary>
        /// 保存用户动态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool Save(UserMovingDto dto)
        {
            try
            {
                var b = userMovingDal.Save(dto);
                return b;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(UserMovingDto dto)", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取小组中用户发表的动态
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<UserMovingDto> GetGroupUserMovings(int groupId)
        {
            try
            {
                List<UserMovingDto> dtos = null;
                var hts = userMovingDal.SelectMovingByGroup(groupId);
                if (hts != null && hts.Count > 0)
                {
                    var userBusiness = new UserBusiness();
                    dtos = new List<UserMovingDto>();
                    foreach (var ht in hts)
                    {
                        var dto = FillDto.FillUserMovingDto(ht);
                        dto.Createor = userBusiness.GetUsersById(dto.MovingUserId);
                        dtos.Add(dto);
                    }
                }
                return dtos;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetGroupUserMovings(int groupId)", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取用户发表的动态
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="userId"></param>
        /// <param name="pageNo"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<UserMovingDto> GetUserMovings(int pageNo, int pageSize, int userId, out int totalCount)
        {
            try
            {
                List<UserMovingDto> dtos = null;
                var hts = userMovingDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageNo * pageSize, userId, "");
                if (hts != null)
                {
                    dtos = new List<UserMovingDto>();
                    foreach (var ht in hts)
                    {
                        dtos.Add(FillDto.FillUserMovingDto(ht));
                    }
                }
                totalCount = userMovingDal.SelectCount(userId);
                return dtos;
            }
            catch (Exception ex)
            {
                totalCount = -1;
                Log.LogHandler.LogError(Waming + ".GetUserMovings(int pageNo, int pageSize, int userId)", ex);
                return null;
            }
        }
    }
}
